# -*- coding: utf-8 -*-
"""
关键字提取模块，处理统一关键字和自定义关键字的提取
"""

import sys
import os

# 添加项目根目录到系统路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from llm import quick_chat
from prompts import get_system_prompt
from common.config import LLM_CONFIG, USE_SILICONFLOW
from common.dict_manager import get_dict_name_cached
from common.clean_llm_result import clean_llm_result


async def extract_tygjz(case_content: str) -> str:
    """提取统一关键字"""
    try:
        # 根据全局配置选择配置
        if USE_SILICONFLOW:
            base_url = LLM_CONFIG["BASE_URL_dev"]
            model = LLM_CONFIG["MODEL_dev"]
            print(f"使用硅基流动配置: {base_url}, 模型: {model}")
        else:
            base_url = LLM_CONFIG["BASE_URL_1238"]
            model = LLM_CONFIG["MODEL_14B"]
            print(f"使用原有配置: {base_url}, 模型: {model}")

        # 根据配置决定是否传递API密钥
        if USE_SILICONFLOW:
            result = quick_chat(
                base_url=base_url,
                model=model,
                system_prompt=get_system_prompt("tygjz"),
                user_prompt=case_content,
                api_key=LLM_CONFIG["API_KEY"],
                temperature=0.5,
            )
        else:
            result = quick_chat(
                base_url=base_url,
                model=model,
                system_prompt=get_system_prompt("tygjz"),
                user_prompt=case_content,
                temperature=0.5,
            )
        raw_result = result.text.strip()
        # 清洗模型输出，移除think标签和Markdown代码块标记
        cleaned_raw_result = clean_llm_result(raw_result)
        # 进行字典转换
        converted_result = get_dict_name_cached("统一关键字", cleaned_raw_result)
        print(f"统一关键字提取结果: {cleaned_raw_result} -> {converted_result}")
        return converted_result
    except Exception as e:
        print(f"提取统一关键字失败: {str(e)}")
        return ""


async def extract_zdygjz(case_content: str) -> str:
    """提取自定义关键字"""
    try:
        # 根据全局配置选择配置
        if USE_SILICONFLOW:
            base_url = LLM_CONFIG["BASE_URL_dev"]
            model = LLM_CONFIG["MODEL_dev"]
            print(f"使用硅基流动配置: {base_url}, 模型: {model}")
        else:
            base_url = LLM_CONFIG["BASE_URL_1238"]
            model = LLM_CONFIG["MODEL_14B"]
            print(f"使用原有配置: {base_url}, 模型: {model}")

        # 根据配置决定是否传递API密钥
        if USE_SILICONFLOW:
            result = quick_chat(
                base_url=base_url,
                model=model,
                system_prompt=get_system_prompt("zdygjz"),
                user_prompt=case_content,
                api_key=LLM_CONFIG["API_KEY"],
                temperature=0.5,
            )
        else:
            result = quick_chat(
                base_url=base_url,
                model=model,
                system_prompt=get_system_prompt("zdygjz"),
                user_prompt=case_content,
                temperature=0.5,
            )
        raw_result = result.text.strip()
        # 清洗模型输出，移除think标签和Markdown代码块标记
        cleaned_raw_result = clean_llm_result(raw_result)
        # 进行字典转换
        converted_result = get_dict_name_cached("自定义关键字", cleaned_raw_result)
        print(f"自定义关键字提取结果: {cleaned_raw_result} -> {converted_result}")
        return converted_result
    except Exception as e:
        print(f"提取自定义关键字失败: {str(e)}")
        return ""
